package com.attendance.controller;

import com.attendance.pojo.Menu;
import com.attendance.result.LayuiMiniPage;
import com.attendance.service.MenuServiceImpl;
import com.attendance.web.Result;
import com.attendance.web.ResultCode;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;

/**
 * 菜单控制器
 *
 * @author 今朝
 * @date 2021/03/14
 */
@Controller
@RequestMapping("/system/menu")
@RolesAllowed("ADMIN")
public class MenuController {

    @Autowired
    MenuServiceImpl menuService;

    @GetMapping()
    public String table() {
        return "/system/menu/table";
    }

    @GetMapping("/edit")
    public String edit() {
        return "/system/menu/edit";
    }

    @GetMapping("/add")
    public String add() {
        return "/system/menu/add";
    }

    @GetMapping("/edit/{id}")
    public ModelAndView edit(@PathVariable("id") Long id) {
        ModelAndView modelAndView = new ModelAndView("/system/menu/edit");
        Menu menu = menuService.selectById(id);
        modelAndView.addObject("menu", menu);
        Menu parent = menuService.selectById(menu.getParentId());
        if (parent != null) {
            modelAndView.addObject("parentName", parent.getTitle());
        } else {
            modelAndView.addObject("parentName", "根节点");
        }
        return modelAndView;
    }

    @GetMapping("/list")
    @ResponseBody
    public Result getList() {
        return new Result(ResultCode.SUCCESS, menuService.getList());
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public Result delete(@PathVariable Long id) {
        try {
            if (menuService.delete(id)) {
                return new Result(ResultCode.SUCCESS);
            } else {
                return new Result(ResultCode.FAIL);
            }
        } catch (Exception e) {
            return new Result(502, e.getMessage(), false);
        }
    }

    @ResponseBody
    @GetMapping("/listPage")
    public LayuiMiniPage<Menu> getListPage(Integer page, Integer limit, String title) {
        if (title != null) {
            Page<Menu> listPage = menuService.getListPage(page, limit, title);
            return new LayuiMiniPage<>(200, "查询成功", listPage.getTotal(), listPage.getRecords());
        }
        Page<Menu> listPage = menuService.getListPage(page, limit);
        return new LayuiMiniPage<>(200, "查询成功", listPage.getTotal(), listPage.getRecords());
    }

    @ResponseBody
    @PostMapping("/update")
    public Result update(Menu menu) {
        if (menuService.updateById(menu)) {
            return new Result(ResultCode.SUCCESS);
        } else {
            return new Result(ResultCode.FAIL);
        }
    }

    @ResponseBody
    @PutMapping("/add")
    public Result add(Menu menu) {
        try {
            if (menuService.addMenu(menu)) {
                return new Result(ResultCode.SUCCESS);
            } else {
                return new Result(ResultCode.FAIL);
            }
        } catch (Exception e) {
            return new Result(502, e.getMessage(), false);
        }
    }

}
